# -*- coding: utf-8 -*-
# @Time : 2023/7/3 13:08:35
# @Author : sacon
# @File : MonitoringOperation.py

import requests,uuid
import time
from loguru import logger


class MonitoringOperation:
    def __init__(self, task_id):
        """
        监控定时任务
        """
        self.task_id = task_id


    def request_handle(self, procedure_id, run_id,Type='request_before'):
        if Type != 'request_before' and Type != 'request_after':
            logger.exception(f'Type参数错误，只能为request_before或者request_after')
            return

        data = {
            'procedure_id': procedure_id,  # 流程id，通过上面的获取所有流程接口获取
            'run_id': run_id  # 运行id，这个是自己生成的，用于后面的请求
        }
        url = f'http://192.168.66.35:8000/{Type}/'

        response = requests.post(url, json=data)

    def __call__(self, fun):
        def retry_handle(*args, **kwargs):
            run_id = str(uuid.uuid4())
            self.request_handle(self.task_id, run_id, Type='request_before')
            fun_res = fun(*args, **kwargs)
            self.request_handle(self.task_id, run_id, Type='request_after')
            return fun_res

        return retry_handle


@MonitoringOperation(2)
def test():
    print('test')
    # dict_ = {"name": "test"}
    # name = dict_["age"]
    # print(name)
    time.sleep(5)
    return 'test'

if __name__ == '__main__':
    test()
    """
    from Monitoring.MonitoringOperation import MonitoringOperation
    @MonitoringOperation(2)
    """